package com.qingneng.budget.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;
import com.qingneng.budget.dao.ICompanyDAO;
import com.qingneng.budget.dao.RoleDAO;
import com.qingneng.budget.dao.UserinfoDAO;
import com.qingneng.budget.vo.Userinfo;

public class GetJsonAction {
	private Logger log = Logger.getLogger(getClass());
	private Gson gson = new Gson();
	private static GetJsonAction instance = new GetJsonAction();

	public static GetJsonAction getInstance() {
		return instance;
	}

	public String getJson(HttpServletRequest request,
			HttpServletResponse response) {
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		String message = "";
		String type = request.getParameter("type") == null ? "" : request
				.getParameter("type");
		if (type.equalsIgnoreCase("companyinfoList")) {
			message = getCompanyList(ac);
		}else if(type.equalsIgnoreCase("userinfoList")){
			message = getUserinfoList(ac);
		}
		return message;
	}

	//得到用户信息
	private String getUserinfoList(ApplicationContext context) {
		UserinfoDAO userinfoDao = (UserinfoDAO) context.getBean("userinfoDao");
		List list = userinfoDao.getUserInfoList();
		for (int i=0;i<list.size();i++) {
			Userinfo userinfo = (Userinfo)list.get(i);
		}
		String message = gson.toJson(list);
		log.info(message);
		return message;
	}
	

	// 得到公司信息列表
	private String getCompanyList(ApplicationContext context) {
		ICompanyDAO companyDao = (ICompanyDAO) context
				.getBean("companyInfoDao");
		List list = companyDao.queryCompanyInfo();
		String message = gson.toJson(list);
		log.info(message);
		return message;
	}

	public static void main(String[] args) {
		GetJsonAction getJson = GetJsonAction.getInstance();
	}
}
